package com.houtai.service.problem;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.problem.ProblemDetailDto;
import com.houtai.bean.dto.problem.ProblemListDto;
import com.houtai.bean.vo.problem.*;
import com.houtai.dao.TsProblemDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProblemService {
    private static Logger logger = Logger.getLogger(ProblemService.class);

    @Autowired
    private TsProblemDao tsProblemDao;
    /**
     * @Purpose:    常见问题列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ProblemListVo
     */
    public ProblemListVo problemList(ProblemListDto dto) {
        String logInfo =  ":problemList:";
        logger.info("======" + logInfo + "begin======");
        ProblemListVo vo = new ProblemListVo();
        try {

            String orderBy = "arti_scort DESC, p_id desc";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<ProblemList> lists = tsProblemDao.findProblemList(dto);
            PageInfo<ProblemList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                vo.setCurrent_page(dto.getCurrent_page());
                vo.setList(list.getList());
                vo.setPage_size(list.getPageSize());
                vo.setTotal_record(list.getTotal());
                vo.setPages(list.getPages());
                vo.setMark("0");
                vo.setTip("查询成功");
            } else {
                vo.setMark("1");
                vo.setTip("无常见问题");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    常见问题详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TsProblemDetailVo
     */
    public TsProblemDetailVo problemDetail(ProblemDetailDto dto) {
        String logInfo = this.getClass().getName() + ":problemDetail:";
        TsProblemDetailVo retInfo = new TsProblemDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsProblemDetail detail = tsProblemDao.findTsProblemDetail(dto.getP_id());
            if (detail != null) {
                retInfo.setDetail(detail);
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("常见问题不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    联系客服
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ContactDetailVo
     */
    public ContactDetailVo contactDetail() {
        String logInfo = this.getClass().getName() + ":contactDetail:";
        ContactDetailVo retInfo = new ContactDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsProblemDetail detail = tsProblemDao.findTsProblemDetail(1);
            retInfo.setArti_content(detail.getArti_content());
            retInfo.setMark("0");
            retInfo.setTip("查询成功");
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    点击满意
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo updateTsProblemSatisfiedSize(ProblemDetailDto dto) {
        String logInfo = this.getClass().getName() + ":updateTsProblemSatisfiedSize:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsProblemDetail detail = tsProblemDao.findTsProblemDetail(dto.getP_id());
            if (detail != null) {
                tsProblemDao.updateTsProblemSatisfiedSize(detail.getP_id());
                retInfo.setMark("0");
                retInfo.setTip("成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("常见问题不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    点击满意
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo updateTsProblemNoSatisfiedSize(ProblemDetailDto dto) {
        String logInfo = this.getClass().getName() + ":updateTsProblemNoSatisfiedSize:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsProblemDetail detail = tsProblemDao.findTsProblemDetail(dto.getP_id());
            if (detail != null) {
                tsProblemDao.updateTsProblemNoSatisfiedSize(detail.getP_id());
                retInfo.setMark("0");
                retInfo.setTip("成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("常见问题不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
